package com.sleepfish.generator.service.impl;

import com.sleepfish.common.exception.ServiceException;
import com.sleepfish.common.utils.SecurityUtils;
import com.sleepfish.generator.domain.GenDatabaseInfo;
import com.sleepfish.generator.domain.GenTable;
import com.sleepfish.generator.domain.GenTableColumn;
import com.sleepfish.generator.mapper.GenDatabaseInfoMapper;
import com.sleepfish.generator.mapper.GenTableColumnMapper;
import com.sleepfish.generator.mapper.GenTableMapper;
import com.sleepfish.generator.service.IGenDatabaseInfoService;
import com.sleepfish.generator.util.GenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fumingchao
 * @Description: 添加数据库表信息
 * @date 2022/2/17 19:21
 */
@Service
public class GenDatabaseInfoServiceImpl implements IGenDatabaseInfoService {

    @Autowired
    private GenDatabaseInfoMapper genDatabaseInfoMapper;

    @Autowired
    private GenTableColumnMapper genTableColumnMapper;
    @Autowired
    private GenTableMapper genTableMapper;

    @Override
    public void savaInfo(GenDatabaseInfo genDatabaseInfo) {
        genDatabaseInfoMapper.saveInfo(genDatabaseInfo);
    }

    @Override
    public List<GenTableColumn> tableColumn(List<GenTable> tableList) {
        String operName = SecurityUtils.getUsername();
        try
        {
            for (GenTable table : tableList)
            {
                String tableName = table.getTableName();
                GenUtils.initTable(table, operName);
                //int row = genTableMapper.insertGenTable(table);

                // 保存列信息
                List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
                return genTableColumns;

            }
        }
        catch (Exception e)
        {
            throw new ServiceException("导入失败：" + e.getMessage());
        }
        return null;
    }

    /**
     * 查找所有数据库表信息
     */
    @Override
    public List<GenTable> selectDbTableList(GenTable genTable) {
        return genDatabaseInfoMapper.selectDbTableList(genTable);
    }

    /**
     * 获取表信息
      * @param tables
     * @return
     */
    @Override
    public List<GenDatabaseInfo> importTableList(String tables) {
        return genDatabaseInfoMapper.importTableList(tables);
    }

    @Override
    public void importGenTable(List<GenTable> tableList,String status) {
        String operName = SecurityUtils.getUsername();
        try
        {
            for (GenTable table : tableList)
            {
                String tableName = table.getTableName();
                GenUtils.initTable(table, operName);
                table.setGenerate(status);
                int row = genTableMapper.insertGenTable(table);
                if (row > 0 && !status.equals("否"))
                {
                    // 保存列信息
                    List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
                    for (GenTableColumn column : genTableColumns)
                    {
                        GenUtils.initColumnField(column, table);

                        if(column.getColumnType().startsWith("bigint") ||
                           column.getColumnType().startsWith("datetime") ||
                           column.getColumnType().startsWith("text") ||
                           column.getColumnType().startsWith("int")){
                           column.setColumnType(column.getColumnType()+"(0)");
                        }
                        genTableColumnMapper.insertGenTableColumn(column);
                    }
                }else if (status.equals("否")){
                    GenTable genTable = genTableMapper.selectGenTableByName(table.getTableName());
                    List<GenTableColumn> genTableColumns = table.getColumns();
                    for (GenTableColumn column : genTableColumns) {
                        column.setColumnType(column.getColumnType()+"("+column.getLen()+")");
                        column.setTableId(genTable.getTableId());
                        GenUtils.initColumnField(column, genTable);
                        column.setColumnName(column.getColumnName()+"_");

                        genTableColumnMapper.insertGenTableColumn(column);
                    }
                }

            }
        }
        catch (Exception e)
        {
            throw new ServiceException("导入失败：" + e.getMessage());
        }
    }

    @Override
    public List<GenTable> selectGenTableList(GenTable genTable) {
        return genDatabaseInfoMapper.selectGenTableList(genTable);
    }

    @Override
    public List<GenTable> selectGenTableListPro(GenTable genTable) {
        List<GenTable> genTables = genDatabaseInfoMapper.selectGenTableList(genTable);
        for (GenTable table : genTables) {
            table.setColumns(genTableColumnMapper.selectGenTableColumnListByTableId(table.getTableId()));
        }
        return genTables;
    }

}
